//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 李东 on 15/12/17.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit


class VisitorLoginView: UIView {
    
    
    override init(frame: CGRect) {
        //调用父类的init
        super.init(frame: frame)
        //在这里添加子视图
        setupSubview()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //外部调用设置访客视图的方法
    func setupVisitorLoginView(tipTitle: String, imageName: String?) {
        //设置提示语
        tipLabel.text = tipTitle
        //根据图片判断是不是首页
        if let name = imageName {
            //不是首页
            circleView.image = UIImage(named: name)
            //隐藏大房子
            largeIcon.hidden = true
            
            
            //将背景图片移动到最上边
//            bringSubviewToFront(backView)
            backView.hidden  = true
            
        } else {
            //是首页
            //就执行动画
            setupCircleViewAnimation()
            
        }
        
        
    }
    
    
    //创建圆盘的动画效果
    private func setupCircleViewAnimation() {
        //设置要改变的属性
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //使用基本动画
        //重复次数
        anim.repeatCount = MAXFLOAT
        //设置循环事件
        anim.duration = 20.0
        //变化量
        anim.toValue = 2 * M_PI
        //设置动画完毕或页面失去活跃状态动画不移除
        anim.removedOnCompletion = false
        //添加动画
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    //添加子控件,设置子控件
    private func setupSubview() {
        //添加子控件
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeIcon)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
//        for subView in subviews {
//            subView.translatesAutoresizingMaskIntoConstraints = false
//        }
        //设置子控件的frame
        //设置 largeIcon的frame
        //大房子的自动布局
        largeIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //圆盘的
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(largeIcon.snp_center)
        }

        //设置提示文的自动布局
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.centerX.equalTo(circleView.snp_centerX)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        //背景
        backView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(600)
            make.bottom.equalTo(loginBtn.snp_bottom)
        }
        //添加按钮点击事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
    }
    
    //登录按钮的点击事件
    @objc private func loginBtnClick() {
        
        
        
    }
    //注册按钮点击事件
    @objc private func registerBtnClick() {
        
        
        
    }
    
    
    //懒加载所有子控件
    //背景
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //大房子
    lazy var largeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //圆圈
    lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //提示语
    lazy var tipLabel: UILabel = UILabel(title: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知", color: UIColor.darkGrayColor(), fontSize: 14)
    //登录按钮
    lazy var loginBtn: UIButton = UIButton(title: "登录", backImageName: "common_button_white_disable", color: themeColor, fontSize: 15)
    //注册按钮
    lazy var registerBtn: UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable", color: themeColor, fontSize: 15)
    
    
}
